package com.yuke.cloud.service.pmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.pmc.dto.CategoryDto;
import com.yuke.cloud.service.pmc.dto.ProdBrandCategoryDto;
import com.yuke.cloud.service.pmc.dto.ProdCategoryBrandDto;
import com.yuke.cloud.service.pmc.entity.ProdBrand;
import com.yuke.cloud.service.pmc.entity.ProdCategory;
import com.yuke.cloud.service.pmc.entity.ProdClass;
import com.yuke.cloud.service.pmc.entity.ProdFactory;
import com.yuke.cloud.service.pmc.service.ProdBrandService;
import com.yuke.cloud.service.pmc.service.ProdCategoryService;
import com.yuke.cloud.service.pmc.service.ProdClassService;
import com.yuke.cloud.service.pmc.service.ProdFactoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by zys on 2018/09/28.
 */
@Slf4j
@Api(value = "WEB - ProdCategoryController", tags = "处理ProdCategory相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/prod/category")
public class MallProdCategoryController extends BaseController {
    @Resource
    private ProdCategoryService prodCategoryService;
    @Resource
    private ProdBrandService prodBrandService;
    @Resource
    private ProdFactoryService prodFactoryService;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;
    @Resource
    private ProdClassService prodClassService;


    @GetMapping("/app")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<CategoryDto>> list(@ApiParam(name = "saleType", value = "商品销售类型", defaultValue = "1") @RequestParam(defaultValue = "1") Byte saleType) {
        List<CategoryDto> list = prodCategoryService.findAllEnableList(saleType);
        return WrapMapper.ok(list);
    }

    /**
     * B 端推荐分类
     *
     * @param
     * @return
     */
    @PostMapping("/selectRecommended")
    @ApiOperation(httpMethod = "POST", value = "App获取热门分类")
    public Wrapper<List<ProdCategory>> selectRecommended() {
        Example example = new Example(ProdCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isEnable", 1);
        criteria.andCondition("is_recommend = 2 or is_recommend = 3");
        List<ProdCategory> prodCategories = prodCategoryService.selectByExample(example);
        return WrapMapper.ok(prodCategories);
    }

    /**
     * B端 裕客商城下的 裕客超市、化妆品、精选鞋包
     * @return
     */
    @GetMapping("/appShop")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<ProdCategory>> listCategory() {
        List<ProdCategory> findAllCategory = prodCategoryService.getFindAllCategory();
        return WrapMapper.ok(findAllCategory);
    }

    /**
     * B端 获取 裕客超市、化妆品、精选鞋包下的分类
     * @param categoryType
     * @return
     */
    @GetMapping("/appCategory")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<CategoryDto>> listAllCategory(@ApiParam(name = "categoryType", value = "分类类型", defaultValue = "1") @RequestParam(defaultValue = "1") Byte categoryType) {
        List<CategoryDto> findCategory = prodCategoryService.getFindCategory(categoryType);
        return WrapMapper.ok(findCategory);
    }

    /**
     * 获取B 端分类，只取二级分类
     * @return
     */
    @GetMapping("/shopSubCategory")
    @ApiOperation(httpMethod = "GET", value = "获取指定类的二级所有记录")
    public Wrapper<List<ProdCategory>> shopCategoryTwoLevel(@ApiParam(name = "categoryId", value = "一级分类categoryId") @RequestParam Long categoryId) {
        Map<String, Object> map = new HashMap<>();
        //获取二级分类
        List<ProdCategory> list = prodCategoryService.findAppCategoryTwoLevel(categoryId,"B");

        return WrapMapper.ok(list);
    }

    /**
     * 获取B 端分类，只取二级分类及二级分类下的品牌
     * @return
     */
    @GetMapping("/shopSubCategoryBrands")
    @ApiOperation(httpMethod = "GET", value = "获取指定类的二级所有记录")
    public Wrapper<Map<String, Object>> shopSubCategoryBrands(@ApiParam(name = "categoryId", value = "一级分类categoryId") @RequestParam Long categoryId) {
        Map<String, Object> map = new HashMap<>();
        //获取二级分类
        List<ProdCategory> list = prodCategoryService.findAppCategoryTwoLevel(categoryId,"B");

        for (ProdCategory prodCategory : list){
            List<ProdBrand> brands = prodBrandService.selectBrandByCategoryId(prodCategory.getCategoryId());
            if (brands != null && brands.size() > 0){
                map.put(prodCategory.getCategoryName(),brands);
            }


        }
        return WrapMapper.ok(map);
    }
    /**
     * C 端热门分类分类图片
     *
     * @param
     * @return
     */
    @PostMapping("/getRecommended")
    @ApiOperation(httpMethod = "POST", value = "App获取热门分类")
    public Wrapper<List<ProdCategory>> getRecommendedCassification() {
        Example example = new Example(ProdCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isEnable", 1);
        criteria.andCondition("is_recommend = 1 or is_recommend = 3");
        List<ProdCategory> prodCategories = prodCategoryService.selectByExample(example);
        return WrapMapper.ok(prodCategories);
    }

    /**
     * 获取C 端推荐分类
     * @return
     */
    @PostMapping("/getClassification")
    @ApiOperation(httpMethod = "POST", value = "App获取推荐分类首页")
    public Wrapper<Map<String, Object>> getClassification() {
        Map<String, Object> map = new HashMap<>();
        System.out.println("获取C 端推荐分类的内容：可口系列+热门分类+国际大牌+推荐品牌");
        //推荐分类的子类别
        Example example3 = new Example(ProdCategory.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("parentId", 296656388603248640L);
        List<ProdCategory> prodCategories1 = prodCategoryService.selectByExample(example3);

        //获取可口可乐等厂家信息,可口系列已修改
        Example example2 = new Example(ProdFactory.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("isEnable", 1);
        criteria2.andEqualTo("isRecommend", 1);
        example2.setOrderByClause("sort ASC");
        List<ProdFactory> coca = prodFactoryService.selectByExample(example2);
        map.put("热销品牌",coca);
        System.out.println("热销品牌" + coca);

        //获取热门分类
        Example example = new Example(ProdCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isEnable", 1);
        criteria.andCondition("is_recommend = 1 or is_recommend = 3");
        example.setOrderByClause("recommend_sort ASC");
        List<ProdCategory> prodCategories = prodCategoryService.selectByExample(example);
        map.put(prodCategories1.get(0).getCategoryName(), prodCategories);

        //获取国际大牌
        Example example1 = new Example(ProdBrand.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("isEnable", 1);
        criteria1.andEqualTo("isRecommend", 1);
        example1.setOrderByClause("recommend_sort ASC");
        List<ProdBrand> prodBrands = prodBrandService.selectByExample(example1);


        for (ProdBrand prodBrand : prodBrands) {
            prodBrand.setCategoryId(0L);
            prodBrand.setFactoryId(0L);
        }
        map.put(prodCategories1.get(1).getCategoryName(), prodBrands);

        //获取推荐品牌
        Example example4 = new Example(ProdBrand.class);
        Example.Criteria criteria4 = example4.createCriteria();
        criteria4.andEqualTo("isEnable", 1);
        criteria4.andEqualTo("isRecommend", 2);
        example4.setOrderByClause("recommend_sort DESC");
        List<ProdBrand> ps = prodBrandService.selectByExample(example4);


        for (ProdBrand prod :ps) {
            prod.setCategoryId(0L);
        }

        map.put(prodCategories1.get(2).getCategoryName(),ps);

        return WrapMapper.ok(map);
    }

    /**
     * 获取B 端分类 裕客商城下的各个分类的品牌
     * @return
     */
    @GetMapping("/getCategoryGoods")
    @ApiOperation(httpMethod = "GET", value = "App获取推荐分类首页")
    public Wrapper<Map<String, Object>> getCategoryGoods(@RequestParam Long categoryId) {

        Long categoryIds=GlobalConstant.PMC_CATEGORY;
        //CategoryDto infoById = prodCategoryService.findInfoById(categoryId);
        Example example1 = new Example(ProdCategory.class);
        Example.Criteria criteria = example1.createCriteria();
        criteria.andEqualTo("parentId",categoryIds);
        List<ProdCategory> prodCategories1= prodCategoryService.selectByExample(example1);
        System.out.println("获取推荐分类"+prodCategories1);
        Map<String, Object> map = new HashMap<>();
        //获取裕客超市的子类别 下的品牌
        if (categoryId !=null &&categoryId.toString().equals(prodCategories1.get(0).getCategoryId().toString())){
            //获取精选品牌
            Example example3 = new Example(ProdBrand.class);
            Example.Criteria criteria3 = example3.createCriteria();
            criteria3.andEqualTo("isEnable", 1);
            criteria3.andEqualTo("isRecommend",2);
            List<ProdBrand> brands = prodBrandService.selectByExample(example3);
            if(brands!=null&&brands.size()>0){
                for (int i = 0; i < brands.size(); i++) {
                    brands.get(i).setCategoryId(null);
                }
            }

            map.put(prodCategories1.get(0).getCategoryName(),brands);
            return WrapMapper.ok( map);
        }
        //获取鞋包分类 的品牌
        if (categoryId != null&&categoryId.toString().equals(prodCategories1.get(1).getCategoryId().toString())){
            List<Long> list = new ArrayList<>();
            List<Long> list1 = new ArrayList<>();
            //获取 时尚女鞋 潮流男鞋  箱包手袋的分类信息
            Example examples = new Example(ProdCategory.class);
            Example.Criteria criterias = examples.createCriteria();
            criterias.andEqualTo("parentId",0);
            criterias.andEqualTo("categoryType",2);
            criterias.andEqualTo("businessView",2);
            List<ProdCategory> prodCategories = prodCategoryService.selectByExample(examples);
            list=prodCategories.stream().map(ProdCategory::getCategoryId).collect(Collectors.toList());

            //根据一级分类Id 找到二级分类下的分类
            Example example4 = new Example(ProdCategory.class);
            Example.Criteria criteria4 = example4.createCriteria();
            criteria4.andIn("parentId", list);
            criteria4.andEqualTo("isEnable" ,1);
            criteria4.andEqualTo("businessView" ,1);
            List<ProdCategory> pc  = prodCategoryService.selectByExample(example4);
            list1=pc.stream().map(ProdCategory::getCategoryId).collect(Collectors.toList());

            //根据分类Id找到对应品牌
            Example example = new Example(ProdBrand.class);
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andIn("categoryId",list1);
            criteria1.andEqualTo("isEnable", 1);
            List<ProdBrand> brands = prodBrandService.selectByExample(example);
            if(brands!=null&&brands.size()>0){
                for (int i = 0; i < brands.size(); i++) {
                    brands.get(i).setCategoryId(null);
                }
            }
            map.put(prodCategories1.get(1).getCategoryName(),brands);
            return WrapMapper.ok(map);
        }
        //获取化妆品 的品牌
        if (categoryId != null&&categoryId.toString().equals(prodCategories1.get(2).getCategoryId().toString())){
            List<Long> list = new ArrayList<>();
            Example example5 = new Example(ProdCategory.class);
            Example.Criteria criteria5 = example5.createCriteria();
            //criteria5.andEqualTo("parentId",264049077028388864L);
            criteria5.andEqualTo("isEnable",1);
            criteria5.andEqualTo("businessView",1);
            criteria5.andEqualTo("level" , 2);
            criteria5.andEqualTo("categoryType",3);
            List<ProdCategory> pps  = prodCategoryService.selectByExample(example5);
            list = pps.stream().map(ProdCategory::getCategoryId).collect(Collectors.toList());

            Example example = new Example(ProdBrand.class);
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andEqualTo("isEnable", 1);
            criteria1.andIn("categoryId",list);
            List<ProdBrand> brands = prodBrandService.selectByExample(example);
            if(brands!=null&&brands.size()>0){
                for (int i = 0; i < brands.size(); i++) {
                    brands.get(i).setCategoryId(null);
                }
            }
            map.put(prodCategories1.get(2).getCategoryName(),brands);
            return WrapMapper.ok(map);
        }

        return WrapMapper.ok( map);
    }

    /**
     * B 端、C 端点击分类品牌获取商品，搜索框搜索商品。
     * @param page
     * @param size
     * @param shopId
     * @param prodCategoryBrandDto
     * @return
     */
    @PostMapping("/getproductpage")
    @ApiOperation(httpMethod = "POST", value = "App根据条件获取商品")
    public Wrapper<PageInfo<ProdCategoryBrandDto>> getProductPages(
                                                                   @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0")Integer page,
                                                                   @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0")Integer size,
                                                                   @ApiParam(name = "shopId", value = "店铺ID") @RequestParam(required = false) Long shopId,
                                                                   @ApiParam(name = "prodCategoryBrandDto", value = "App根据条件获取商品") @RequestBody(required = false) ProdCategoryBrandDto prodCategoryBrandDto) {
        List<ProdCategoryBrandDto>list = new ArrayList<>();

        System.out.println("App根据条件获取商品"+prodCategoryBrandDto);
        if (prodCategoryBrandDto != null && prodCategoryBrandDto.getSaleType().toString().equals("1")){
            PageHelper.startPage(page, size);
             list = prodCategoryService.getProdCategoryBrand(prodCategoryBrandDto, shopId);
        }else
        if (prodCategoryBrandDto != null && prodCategoryBrandDto.getSaleType().toString().equals("2")){
            PageHelper.startPage(page, size);
             list = prodCategoryService.getProdBrandCategory(prodCategoryBrandDto, shopId);
        }

        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 由分类categoryId获得品牌信息，+ 由brandId获得类型信息
     * @return
     */
    @PostMapping("/getbrandcategory")
    @ApiOperation(httpMethod = "POST", value = "App筛选商品或分类")
    public Wrapper<List<ProdBrandCategoryDto>>  getbrandcategory(@ApiParam(name = "prodCategoryBrandDto", value = "App根据条件筛选商品或分类") @RequestBody ProdCategoryBrandDto prodCategoryBrandDto) {
        System.out.println("由分类categoryId获得品牌信息:"+prodCategoryBrandDto);
        List<ProdBrandCategoryDto> prodBrandCategoryDtos = prodCategoryService.getFilter(prodCategoryBrandDto);
        return WrapMapper.ok(prodBrandCategoryDtos);
    }



    /**
     * 获取C 端分类，只取一级分类,B 端 saleType=2
     * @return
     */
    @GetMapping("/clientCategory")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<ProdCategory>> appCategoryOneLevel(@ApiParam(name = "saleType", value = "商品销售类型", defaultValue = "1") @RequestParam(defaultValue = "1") Byte saleType) {
        List<ProdCategory> list = prodCategoryService.findAppCategoryOneLevel(saleType);
        return WrapMapper.ok(list);
    }

    /**
     * 获取C 端分类，只取二级分类,和分类下的品牌
     * @return
     */
    @GetMapping("/clientSubCategory")
    @ApiOperation(httpMethod = "GET", value = "获取指定类的二级所有记录")
    public Wrapper<Map<String, Object>> appCategoryTwoLevel(@ApiParam(name = "categoryId", value = "一级分类categoryId") @RequestParam Long categoryId) {

        Map<String, Object> map = new HashMap<>();
        //获取二级分类
        List<ProdCategory> list = prodCategoryService.findAppCategoryTwoLevel(categoryId,"C");

        for (ProdCategory prodCategory : list){
            List<ProdBrand> brands = prodBrandService.getProdBrandByCategoryIds(prodCategory.getCategoryId());
            if (brands != null && brands.size() > 0){
                map.put(prodCategory.getCategoryName(),brands);
            }
        }
        return WrapMapper.ok(map);
    }

    /**
     * 获取C 端分类，只取二级分类,和分类下的三级分类
     * @return
     */
    @GetMapping("/classSubCategory")
    @ApiOperation(httpMethod = "GET", value = "获取指定类的二级所有记录")
    public Wrapper<Map<String, Object>> appClassSubCategory(@ApiParam(name = "categoryId", value = "一级分类categoryId") @RequestParam Long categoryId) {

        Map<String, Object> map = new HashMap<>();
        //获取二级分类
        List<ProdClass> list = prodClassService.findClassSubCategory(categoryId,"C");
        if (list == null || list.size() < 1){
            return WrapMapper.error("未找到二级分类！");
        }
        for (ProdClass prodClass : list){
            List<ProdCategory> categoryList = prodCategoryService.findCategoryByClassId(prodClass.getClassId(),"C");
            if (categoryList != null && categoryList.size() > 0){
                map.put(prodClass.getClassName(),categoryList);
            }
        }
        return WrapMapper.ok(map);
    }

    /**
     * 由分类categoryIdList获得品牌信息brandIdList
     * @return
     */
    @PostMapping("/getBrandByCategoryList")
    @ApiOperation(httpMethod = "POST", value = "App筛选商品或分类")
    public Wrapper<List<ProdBrand>>  getbrandcategory(@ApiParam(name = "prodCategoryBrandDto", value = "App根据条件筛选商品或分类") @RequestBody List<Long> prodCategories) {
        if (prodCategories != null &&  prodCategories.size() > 0){

            Example example = new Example(ProdBrand.class);
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andEqualTo("isEnable", 1);
            criteria1.andIn("categoryId",prodCategories);
            List<ProdBrand> brands = prodBrandService.selectByExample(example);
            if(brands != null && brands.size() > 0){
                return WrapMapper.ok(brands);
            }

        }

        return WrapMapper.error("参数错误或没有找到品牌信息");
    }

    @PostMapping("/getBrand")
    @ApiOperation(httpMethod = "POST", value = "App筛选商品或分类")
    public Wrapper<List<ProdBrand>>  getbrand(@ApiParam(name = "prodCategories", value = "App根据条件筛选品牌") @RequestBody List<Long> prodCategories,
                                              @ApiParam(name = "specName", value = "specName") @RequestParam String specName,
                                              @ApiParam(name = "saleType", value = "saleType") @RequestParam Byte saleType,
                                              @ApiParam(name = "shopId", value = "shopId") @RequestParam Long shopId ) {
        if (prodCategories == null &&  prodCategories.size() < 1) {
            return WrapMapper.error("参数错误或没有找到品牌信息");
        }

        List<Long> prodBrands = wmcStorageFeignApi.getBrandsByCategorys(prodCategories,specName,saleType,shopId);

        if (prodBrands == null || prodBrands.size() < 1){
           return getbrandcategory(prodCategories);
        }

        Example example = new Example(ProdBrand.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.andEqualTo("isEnable", 1);
        criteria1.andIn("brandId",prodBrands);
        List<ProdBrand> brands = prodBrandService.selectByExample(example);
        if(brands != null && brands.size() > 0){
            return WrapMapper.ok(brands);
        }


        return WrapMapper.error("参数错误或没有找到品牌信息");

    }
}
